package com.example.lib_car

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ParkingViewModel : BaseViewModel<ParkingRepository,ParkingState>() {
    override fun createRepo(): ParkingRepository {
        return ParkingRepository()
    }
    var list : ArrayList<ParkingEntity> =ArrayList()
    override fun createState(): ParkingState {
        return ParkingState(ResponseEntity(0,"",list))
    }

    fun parkingAll(){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestAllSpace().flowOn(Dispatchers.IO)
                .catch { t:Throwable->
                }
                .collect{
                    mutableContainer.updateRequestState {
                        copy(request=false,complete = true)
                    }
                    mutableContainer.updateUiState {
                        copy(entity=it)
                    }
                }
        }
    }
}